<!--
 * @Description: 
 * @Author: dh
 * @Date: 2021-09-27 16:11:28
 * @LastEditors: dh
 * @LastEditTime: 2021-09-27 16:17:56
-->
<!doctype html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
	</head>

	<body>
		<!-- 这里不需要script，其他标签一样可以，如果是script标签，type 要浏览器不能识别的才行 -->
		<script id="worker" type="app/worker">
			addEventListener('message', function (e) {
			    console.log("%cworker-收到消息：" + e.data, "color:blue");
			    postMessage('收到咯');
			}, false);
		</script>

		<script>
			// 获取 worker 字符串文本
			const workerContent = document.querySelector('#worker').textContent;

			// 生成二进制
			var blob = new Blob([workerContent]);

			// 创建本地url
			var url = window.URL.createObjectURL(blob);

			var worker = new Worker(url);

			worker.postMessage('ok');

			worker.onmessage = function (e) {
				console.log('worker发出消息：' + e.data);
			};
		</script>
	</body>
</html>
